﻿<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="utf-8" %>
<script runat="server">
	string inst = "";
	void Page_Load(object o, EventArgs e) {
		if(Request.Params["ifile"]!=null)
			inst = Request.Params["ifile"].ToString();
			
		if(inst.Trim()=="") return;
		object odown = null;
		
		
		odown = ACM.cmsarea08.DBManager.mainConnection.HStandard_SC("select concat(pname,'~',srcname) as fileme from "+ACM.cmsarea08.DBManager.mainConnection.TablePrefix+"admfilemanager where id="+inst+" limit 1;");
		if(odown==null) {
			Response.Write("<script language=\"javascript\">alert('Impossibile scaricare il file');</"+"script>");
			return;		
		}
		string filename = "";
		if(odown is byte[])  filename = System.Text.Encoding.UTF8.GetString((byte[]) odown) ;
		else filename = odown.ToString();
		
		string srcfile = "";
		string pubfile = "";
		if(filename.IndexOf('~') == -1) { 
			Response.Write("<script language=\"javascript\">alert('Impossibile scaricare il file');</"+"script>");
			return;		
		}
		pubfile = filename.Substring(0,filename.IndexOf('~') );
		srcfile = filename.Substring(filename.IndexOf('~') +1 );
		
		Response.Write("<script language=\"javascript\">alert('Impossibile scaricare il file:" + pubfile + "');</"+"script>");

		System.IO.FileInfo fi = new System.IO.FileInfo(Server.MapPath("/public/admfmanager/" + srcfile) );
		if(fi.Exists) {
			Response.Clear();
			Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(pubfile) );
			Response.AddHeader("Content-Length", fi.Length.ToString());
			Response.ContentType = "application/octet-stream";
			Response.WriteFile(Server.MapPath("/public/admfmanager/" + srcfile) );
			Response.End();		
		}
	}


</script>
